
package com.dstz.form.rest.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.utils.HttpConstant;

/** 
 *  @comment 首页压测爬虫
 *  @author xianggang
 *  
 */
@Service
public class AgilebpmTestProcessor implements PageProcessor {
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private static String ctx = "";
	
	@Override
	public void process(Page page) {
		
		String currentUrl = page.getUrl().toString();
		
		logger.info("开始爬取,url:{}", currentUrl);
		
		boolean isLogin = currentUrl.contains("login/valid");
		
		if (!isLogin) {
			logger.info("响应数据,,data:{}", page.getRawText());
			return;
		}
		
		String i = getParam(page, "i");
		
		// 如果是登录,则打开继续请求首页的相关接口
		addRequest(ctx + "/agilebpm-ui/index.html?v=" + i, page);
		
		addRequest(ctx + "/agile-bpm-platform/org/userResource/userMsg?v=" + i, page);
		
		addRequest(ctx + "/agile-bpm-platform/oa/oAInstationMsg/getInstationMsg?type=2&pageNo=1&pageSize=5&v=" + i,
		        page);
		
		addRequest(ctx + "/agile-bpm-platform/bpm/my/getMyFlowCounts?v=" + i, page);
		
		addRequest(ctx + "/agile-bpm-platform/oa/oANews/listJson?v=" + i, page);
		
		addRequest(ctx
		        + "/agile-bpm-platform/oa/oANotify/queryCustomerNotify?oa_Notify.status_%24VEQ=1&offset=0&limit=10&v="
		        + i, page);
		
		addRequest(ctx + "/agile-bpm-platform/org/user/listJson?limit=999&offset=0&v" + i, page);
		
		addRequest(ctx + "/agile-bpm-platform/sys/workbenchPanel/getMyWorkbench?v=" + i, page);
		
		addRequest(ctx + "/agile-bpm-platform/org/user/get?id=1&v=" + i, page);
		
		addRequest(ctx + "/agile-bpm-platform/calendar/schedule/getEvents?start=1567296000000&end=1570924800000&v=" + i,
		        page);
		
		addRequest(ctx
		        + "/agile-bpm-platform/calendar/schedule/getParticipantEvents?start=1567296000000&end=1570924800000&v="
		        + i, page);
		
		addRequest(ctx
		        + "/agile-bpm-platform/sys/workbenchPanel/getPanelData?panelId_=10000051360122&dataType_=interface&dataSource_=workbenchPanelManager.getTestData&v="
		        + i, page);
		
		addRequest(ctx
		        + "/agile-bpm-platform/sys/workbenchPanel/getPanelData?panelId_=10000049030124&dataType_=interface&dataSource_=bpmTaskManager.getTodoList&v="
		        + i, page);
		
		addRequest(ctx
		        + "/agile-bpm-platform/sys/workbenchPanel/getPanelData?panelId_=10000049030124&dataType_=interface&dataSource_=bpmTaskManager.getTodoList&v="
		        + i, page);
		
	}
	
	@Override
	public Site getSite() {
		
		// 每次爬取异常则重试1次,每次重试停顿1秒,每个爬取之间停顿0.1秒
		Site site = Site.me().setCycleRetryTimes(3).setSleepTime(100).setTimeOut(30000).setRetrySleepTime(1000);
		// 将获取到的cookie信息添加到webmagic中
		
		return site;
	}
	
	public void startRep(int threadSize, int invokeTimes) {
		
		List<Request> requestList = new ArrayList<>();
		for (int i = 0; i < invokeTimes; i++) {
			Request request = getRequest(ctx + "/agile-bpm-platform/org/login/valid?account=admin&password=1&v=" + i);
			param(request, "i", i);
			requestList.add(request);
		}
		
		Request[] requestArray = new Request[requestList.size()];
		
		// 设置爬虫
		Spider spider = Spider.create(this).addRequest(requestList.toArray(requestArray)).thread(threadSize);
		
		// 开始爬取
		spider.run();
		
		logger.info("爬取结束");
		
	}
	
	public static Request getRequest(String url) {
		Request request = new Request();
		request.setUrl(url);
		request.setMethod(HttpConstant.Method.GET);
		return request;
	}
	
	public static void addRequest(String url, Page page) {
		page.addTargetRequest(getRequest(url));
	}
	
	public static String getParams(Map<String, Object> map) {
		
		Set<String> keySet = map.keySet();
		String url = "";
		for (String key : keySet) {
			url += key + "=" + map.get(key) + "&";
		}
		if (StringUtils.isEmpty(url)) {
			return url;
		}
		return url.substring(0, url.length() - 1);
	}
	
	public static void param(Request request, String key, String value) {
		Map<String, Object> extras = request.getExtras();
		if (extras == null) {
			extras = new HashMap<>();
		}
		extras.put(key, value);
		request.setExtras(extras);
	}
	
	public static void param(Request request, String key, Object value) {
		Map<String, Object> extras = request.getExtras();
		if (extras == null) {
			extras = new HashMap<>();
		}
		extras.put(key, value);
		request.setExtras(extras);
	}
	
	public static String getParam(Page page, String key) {
		Map<String, Object> extras = page.getRequest().getExtras();
		if (extras == null) {
			extras = new HashMap<>();
		}
		
		return extras.get(key).toString();
	}
	
	// 首页地址 : http://test1.agilebpm.cn/dashboard/console
	public static void main(String[] args) throws Exception {
		
		AgilebpmTestProcessor processor = new AgilebpmTestProcessor();
		
		ctx = "http://test1.agilebpm.cn"; // 测试环境
		
		processor.startRep(1, 1);
		//		processor.startRep(50, 1000);// 测试50个线程,1000次登录
				processor.startRep(100, 1000);// 测试100个线程,1000次登录
		//		processor.startRep(200, 1000);// 测试200个线程,1000次登录
	}
}